package com.bcu.wish.controller;

import com.bcu.wish.entity.Message;
import com.bcu.wish.entity.Time;
import com.bcu.wish.service.TimeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Api(tags = "时间模块")
@RestController
@RequestMapping("/api/Time")
public class TimeController {
    @Resource
    private TimeService timeService;

    @PostMapping("/")
    @ApiOperation(value = "新添时间")
    public Message insert(Time time) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
        Date limitdate = simpleDateFormat.parse(time.getTimeLimittimetostring());
        time.setTimeLimittime(limitdate);
        return timeService.insertSelective(time);
    }

    @GetMapping("/one")
    @ApiOperation(value = "根据时间id查看时间")
    public Message<Time> selectById(int id){
        return timeService.selectByPrimaryKey(id);
    }

    @GetMapping("/all")
    @ApiOperation(value = "查看所有时间")
    public Message<List<Time>> selectALL(){
        return timeService.selectAll();
    }

    @DeleteMapping("/")
    @ApiOperation(value = "根据id删除时间")
    public Message deleteById(int id){
        return timeService.deleteByPrimaryKey(id);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改时间")
    public Message update(Time time) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
        Date limitdate = simpleDateFormat.parse(time.getTimeLimittimetostring());
        time.setTimeLimittime(limitdate);
        return timeService.updateByPrimaryKeySelective(time);
    }


}
